package org.example.myleet.p901;

import java.util.Deque;
import java.util.LinkedList;

public class StockSpanner {

    static class PriceSpan {
        int price;
        int span;

        public PriceSpan(int price, int span) {
            this.price = price;
            this.span = span;
        }
    }

    private Deque<PriceSpan> descStack;

    public StockSpanner() {
        descStack = new LinkedList<>();
    }

    public int next(int price) {
        if (descStack.isEmpty()) {
            descStack.push(new PriceSpan(price, 1));
        } else {
            int span = 1;
            while (!descStack.isEmpty()) {
                PriceSpan peek = descStack.peek();
                if (peek.price <= price) {
                    span += peek.span;
                    descStack.pop();
                } else {
                    break;
                }
            }
            descStack.push(new PriceSpan(price, span));
        }
        return descStack.peek().span;
    }
}
